<?php

namespace Jb\Navigation;

use Jb\Object,
    Jb\Utils\Web\Uri,
    Jb\Utils\Comparison,
    Jb\Utils\Comparison\Comparable;

abstract class Navigation implements Comparable {

    private $sLabel;
    private $oUri;
    private $aRoutes = array();
    private $bActive = false;
    private $oParent;

    private static $oActivePage;

    public function __construct($pageLabel) {
        $this->sLabel = $pageLabel;
    }

    public function addRoute(Route $pageRoute) {
        \array_push($this->aRoutes, $pageRoute);
    }

    public function getRoutes() {
        return $this->aRoutes;
    }

    protected function hasRoutes() {
        if(count($this->aRoutes) > 0) {
            return true;
        } else {
            return false;
        }
    }

    public function getLabel() {
        return $this->sLabel;
    }

    public function setUri(Uri $pageUri) {
        $this->oUri = $pageUri;
    }

    public function getUri() {
        return $this->oUri;
    }

    public function setActive($isActive) {
        $this->bActive = $isActive;

        if($isActive === true && $this->hasParent()) {
            $parentMenu = $this->getParent();
            $parentMenu->setActive(true);
        }
    }

    public function isActive() {
        return $this->bActive;
    }

    public function compareTo(Object $currentRoute) {
        foreach($this->getRoutes() as $oRoute) {
            if($oRoute->compareTo($currentRoute) === true) {
                $this->setActive(true);
                self::$oActivePage = $this;
                //return $this;
            }
        }
    }

    public function getActivePage() {
        return self::$oActivePage;
    }

    public abstract function addPage(Navigation $navigationPage);
    public abstract function removePage(Navigation $navigationPage);

    /**
     * Return the parent (composite)view
     *
     * @return CompositeView|null
     */
    public function getParent() {
        if (empty($this->oParent)) {
            return $this;
        }

        return $this->oParent;
    }

    public function hasParent() {
        if (empty($this->oParent)) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * Set the parent CompositeView object
     *
     * @param oView The CompositeView object that should act as parent
     */
    public function setParent(Navigation $menuPage) {
        $this->oParent = $menuPage;
    }

    /**
     * Checks if the current instance is a composite that can contain childviews
     *
     * @return boolean
     */
    public function isParent() {
        return (boolean) ($this instanceof Menu);
    }

}
?>
